<?php
require_once("./define/define_table.php");

define("ADD_COMMENT_EVENEMENT_FORM", "Formulaire d'ajout de commentaire");
define("MODIFY_COMMENT_EVENEMENT_FORM", "Formulaire de modificiation de commentaire");

class CommentEvenement {

	private $id_commentaire;
	private $id_individu;
	private $id_evenement;
	private $date_commentaire;
	private $commentaire;

	public function CommentEvenement(){
	}

	public function getIdCommentaire(){
		return $this->id_commentaire;
	}

	public function getIdIndividu(){
		return $this->id_individu;
	}

	public function getIdEvenement(){
		return $this->id_evenement;
	}

	public function getDate() {
		return $this->date_commentaire;
	}

	public function getCommentaire(){
		return $this->commentaire;
	}

	public function setIdCommentaire($id){
		$this->id_commentaire = $id;
	}

	public function setIdIndividu($id){
		$this->id_individu = $id;
	}

	public function setIdEvenement($id){
		$this->id_evenement = $id;
	}

	public function setDate($date) {
		$this->date_commentaire = $date;
	}

	public function setCommentaire($commentaire){
		$this->commentaire= $commentaire;
	}
	
	public function getAllComment($id){
	    global $db;
		try{
			$sql = "SELECT * FROM ".COMMENT_EVENEMENT_TABLE." WHERE id_evenement = :id_evenement";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_evenement", $id, PDO::PARAM_INT);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "CommentEvenement");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function getNbComment($id){
	    global $db;
	    try{
	        $sql = "SELECT COUNT(*) FROM ".COMMENT_EVENEMENT_TABLE." WHERE id_evenement = :id_evenement";
	        $sth = $db->prepare($sql);
	        $sth->bindValue(":id_evenement", $id, PDO::PARAM_INT);
	        $sth->execute();
	        $nb = $sth->fetch();
	        return $nb[0];
	    }catch(PDOException $e){
	        echo $e->getMessage();
	    }
	}
	
	public function getComment($id){
	    global $db;
	    try{
	        $sql = "SELECT * FROM ".COMMENT_EVENEMENT_TABLE." WHERE id_commentaire = :id_commentaire";
	        $sth = $db->prepare($sql);
	        $sth->bindValue(":id_commentaire", $id, PDO::PARAM_INT);
	        $sth->execute();
	        return $sth->fetchObject("CommentEvenement");
	    }catch(PDOException $e){
	        echo $e.getMessage();
	    }
	}
	
	public function addOrModify(){
		global $db;
		try{
			if($this->id_commentaire == null){
				$sql = "INSERT INTO ".COMMENT_EVENEMENT_TABLE." VALUES(null, :id_evenement, :id_individu, CURDATE(), :commentaire)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
				$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
				$sth->bindValue(":commentaire", $this->commentaire, PDO::PARAM_STR);
			}
			else {
				$sql = "UPDATE ".COMMENT_EVENEMENT_TABLE." SET commentaire = :commentaire WHERE id_commentaire = :id_commentaire";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_commentaire", $this->id_commentaire, PDO::PARAM_INT);
				$sth->bindValue(":commentaire", $this->commentaire, PDO::PARAM_STR);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e.getMessage();
		}

	}

	public function getAddForm(){
		$html = ADD_COMMENT_EVENEMENT_FORM.
			'<form action="?module=commentEvenement&action=doAdd" method="post">' .
			'	<table>' .
			'		<tr>' .
			'			<td>'._ID_EVENEMENT_.'</td>' .
			'			<td><input readonly type="text" name="id_evenement" value = "'.$this->id_evenement.'"/></td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td>'._ID_INDIVIDU_.'</td>' .
			'			<td>'.Adherent::generateSelect().'</td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td>'._COMMENTAIRE_.'</td>' .
			'			<td><input type="text" name="commentaire" /></td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td colspan="2"><input type="submit" />' .
			'		</tr>' .
			'	</table>' .
			'</form>';
		return $html;
	}

	public function getModifyForm(){
		$html = MODIFY_COMMENT_EVENEMENT_FORM.
			'<form action="?module=commentEvenement&action=doModify" method="post">' .
			'	<table>' .
			'		<tr>' .
			'			<td>'._ID_COMMENTAIRE_.'</td>' .
			'			<td><input readonly type="text" name="id_commentaire" value = "'.$this->id_commentaire.'"/></td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td>'._ID_EVENEMENT_.'</td>' .
			'			<td><input readonly type="text" name="id_evenement" value = "'.$this->id_evenement.'"/></td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td>'._ID_INDIVIDU_.'</td>' .
			'			<td><input readonly type="text" name="id_individu" value = "'.$this->id_individu.'"/></td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td>'._COMMENTAIRE_.'</td>' .
			'			<td><input type="text" name="commentaire" value = "'.$this->commentaire.'"/></td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td colspan="2"><input type="submit" />' .
			'		</tr>' .
			'	</table>' .
			'</form>';
		return $html;
	}

	public function delete(){
		global $db;
		$sql = "DELETE FROM ".COMMENT_EVENEMENT_TABLE." WHERE id_commentaire = :id_commentaire";
		$sth = $db->prepare($sql);
		$sth->bindValue(":id_commentaire", $this->id_commentaire, PDO::PARAM_INT);
		$sth->execute();
		PrintQuery::queryPrint($sth);
	}

}

?>
